Popup.initEvents   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
import {define} from "../globals/globals";
2
import {PopupInterface} from "../interfaces/PopupInterface";
3
import {PopupOptions} from "../types/PopupOptions";
4
5
(function (root, factory) {
6
    if (typeof define === 'function' && define.amd) {
7
        define([], factory);
8
    } else if (typeof module === 'object' && module.exports) {
9
        module.exports = factory();
10
    } else {
11
        root!.Popup = factory();
12
    }
13
}(typeof self !== 'undefined' ? self : this, function () {
14
15
    class Popup implements PopupInterface {
16
        public options: PopupOptions;
17
18
        constructor(options:PopupOptions) {
19
20
            if (!options.el) {
21
                throw new Error('No popup root selector')
22
            }
23
            if (!options.openers) {
24
                throw new Error('No popup openers')
25
            }
26
27
            this.options = options;
28
            this.options.el = typeof options.el === 'string' ? document.querySelector(options.el) : options.el;
29
            this.options.openers = typeof options.openers === 'string' ? document.querySelectorAll(options.openers) : options.openers;
30
            this.options.closable = options.closable || false;
31
            this.onInit();
32
            this.onLoad();
33
        }
34
35
        protected onInit(): void {
36
            this.initHTML();
37
            this.initEvents();
38
            this.initCloseIcon();
39
        }
40
41
        protected onLoad(): void {
42
            if(this.options && (this.options.onLoad as Function)) {
43
                this.options.onLoad!();
44
            }
45
        }
46
47
        private initHTML(): HTMLElement {
48
            this.options.el.classList.add('popup');
49
            this.options.el.firstElementChild.classList.add('popup__container');
50
51
            if (this.options.closable) {
52
                let popupClose = document.createElement('button');
53
                popupClose.classList.add('popup__close-btn');
54
                this.options.el.firstElementChild.appendChild(popupClose);
55
            }
56
57
            document.body.appendChild(this.options.el);
58
59
            return this.options.el;
60
        }
61
62
        protected initEvents(): void {
63
            this.open();
64
            this.close();
65
        }
66
67
        protected initCloseIcon(): void {
68
            if(this.options.closable && this.options.closeIcon) {
69
                let closeBtn = this.options.el.querySelector('.popup__close-btn');
70
                closeBtn.innerHTML = this.options.closeIcon;
71
            }
72
        }
73
74
        public open(): void {
75
            let that = this;
76
77
            that.options.openers.forEach(function (popupOpen:HTMLElement) {
78
                popupOpen.addEventListener('click', function (e: Event) {
79
                    e.preventDefault();
80
                    that.options.el.classList.add('active');
81
                    if(that.options.onOpen) {
82
                        that.options.onOpen(e);
83
                    }
84
                })
85
            })
86
        }
87
88
        public close(): void {
89
            let that = this;
90
91
            document.addEventListener('keydown', function(e: KeyboardEvent) {
92
                if(e.key === "Escape") {
93
                    that.options.el.classList.remove('active');
94
95
                    if(that.options.onClose) {
96
                        that.options.onClose.call(that);
97
                    }
98
                }
99
            });
100
101
            that.options.el.addEventListener('click', function (e: Event) {
102
103
                if (!(e.target! as Element).closest('.popup__container')) {
104
                    that.options.el.classList.remove('active');
105
106
                    if(that.options.onClose) {
107
                        that.options.onClose.call(that);
108
                    }
109
                }
110
            });
111
112
            let popupClose = that.options.el.querySelector('.popup__close-btn');
113
114
            if (popupClose) {
115
                popupClose.addEventListener('click', function (e: Event) {
116
                    e.preventDefault();
117
                    that.options.el.classList.remove('active');
118
119
                    if(that.options.onClose) {
120
                        that.options.onClose.call(that);
121
                    }
122
                })
123
            }
124
        }
125
126
        public manualOpen() {
127
            this.options.el.classList.add('active');
128
        }
129
130
        public manualClose() {
131
            this.options.el.classList.remove('active');
132
        }
133
    }
134
135
    return Popup;
136
}));